<template>
  <div class="register-container">
    <!-- 左侧图片区域 -->
    <div class="register-image">
      <img src="@/assets/logo.png" alt="Register Image" />
    </div>

    <!-- 右侧注册框 -->
    <div class="register-box">
      <h1>注册</h1>
      <form @submit.prevent="handleRegister" class="register-form">
        <div class="form-group">
          <label for="username">用户名</label>
          <input
            type="text"
            id="username"
            v-model="form.username"
            required
            class="input-field"
          />
        </div>
        <div class="form-group">
          <label for="email">邮箱</label>
          <input
            type="email"
            id="email"
            v-model="form.email"
            required
            class="input-field"
          />
        </div>
        <div class="form-group">
          <label for="password">密码</label>
          <input
            type="password"
            id="password"
            v-model="form.password"
            required
            class="input-field"
          />
        </div>
        <div class="form-group">
          <label for="confirmPassword">确认密码</label>
          <input
            type="password"
            id="confirmPassword"
            v-model="form.confirmPassword"
            required
            class="input-field"
          />
        </div>
        <div v-if="errorMessage" class="error-message">{{ errorMessage }}</div>
        <button type="submit" class="register-button">注册</button>
      </form>
    </div>

    <!-- 虚拟过渡效果 -->
    <div class="virtual-transition"></div>
  </div>
</template>

<script>
import axios from "@/utils/axios";

export default {
  name: "userRegister",
  data() {
    return {
      form: {
        username: "",
        email: "",
        password: "",
        confirmPassword: "",
      },
      errorMessage: "",
    };
  },
  methods: {
    async handleRegister() {
      if (this.form.password !== this.form.confirmPassword) {
        this.errorMessage = "密码和确认密码不一致";
        return;
      }

      try {
        const response = await axios.post("/register", this.form);
        this.$router.push("/login"); // 注册成功后跳转到登录页
        console.log(response);
      } catch (error) {
        this.errorMessage =
          error.response.data.error || "注册失败，请稍后再试";
      }
    },
  },
};
</script>

<style scoped>
.register-container {
  display: flex;
  height: 100vh;
  background: #f0f4f8;
}

/* 左侧图片 */
.register-image {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #e9ecef;
}

.register-image img {
  width: 80%;
  max-width: 600px;
  height: auto;
}

/* 右侧注册框 */
.register-box {
  width: 400px;
  padding: 20px;
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  margin: auto;
}

h1 {
  text-align: center;
  color: #333;
  margin-bottom: 20px;
  font-size: 24px;
}

.register-form .form-group {
  margin-bottom: 15px;
}

.register-form label {
  display: block;
  margin-bottom: 5px;
  color: #555;
  font-size: 14px;
}

.input-field {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
  font-size: 14px;
}

.input-field:focus {
  outline: none;
  border-color: #007bff;
  box-shadow: 0 0 3px rgba(0, 123, 255, 0.5);
}

.error-message {
  color: #e74c3c;
  font-size: 14px;
  text-align: center;
  margin-bottom: 10px;
}

.register-button {
  width: 100%;
  padding: 10px;
  background: #007bff;
  color: #fff;
  border: none;
  border-radius: 5px;
  font-size: 16px;
  cursor: pointer;
  transition: background 0.3s;
}

.register-button:hover {
  background: #0056b3;
}

/* 虚拟过渡效果 */
.virtual-transition {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 50px;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.1), rgba(255, 255, 255, 0));
  animation: fadeUp 3s ease-out infinite;
}

@keyframes fadeUp {
  0% {
    opacity: 0;
  }
  50% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}
</style>
